<?php

namespace zhtfrontend\controllers;

use doumiaoduo\buildform\BuildForm;
use zhtfrontend\components\bases\BaseController;
use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use common\models\datas\Customer;
use common\models\datas\IsuserInfo;
use yii\data\Pagination;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\web\BadRequestHttpException;
use yii\web\Response;
use zhtfrontend\models\datas\Mechanic;

class IsuserinfoController extends BaseController
{

    public function actionIndex()
    {
        $param['searchData'] = \Yii::$app->request->get();
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
        $data = IsuserInfo::find()->orderBy('id asc')->where(['status' => 1, 'site_id' => Fun::getSiteId()]);
        if (!empty($param['searchData'])) {
            $search_data = array_filter($param['searchData']);
            if (!empty($search_data['mobile'])) {
                $data->andWhere(['like', 'mobile', $search_data['mobile']]);
            }
        }
        if (isset(\Yii::$app->user->identity) && \Yii::$app->user->identity->role == 'business_director') {
            $data->andWhere(['response_man' => \Yii::$app->user->id]);
        }
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();

        $array = Fun::asArray($model, ['admin_name', 'backend_operate', 'response']);

        return $this->render('index', ['data' => $array,'pages'=>$pages]);
    }

    public function actionCreate()
    {
        $this->layout = false;
        $model = new IsuserInfo();
        $model->scenario = 'create';
        $form = new BuildForm('@app/components/forms/isuserinfoForm.php');
        $form->loadModel(['isuserinfo' => $model]);
        $form->loadDefaultValues();
        $admin = \zhtfrontend\models\datas\Admin::find()->where(['role' => ['admin', 'administrator', 'business_director']])->all();
        $mechanic = Mechanic::find()->where(['site_id' => Fun::getSiteId(), 'is_delete' => 0])->all();
        $form->setItems('isuserinfo', 'response_man', ArrayHelper::map($admin, 'id', 'name'));
        $form->setItems('isuserinfo', 'name', ArrayHelper::map($mechanic, 'name', 'name'));
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            $model->user_id = \Yii::$app->user->id;
            $model->site_id = Fun::getSiteId();
            $model->password = md5($model->password);
            if ($model->save(false)) {
                return $this->redirectMessage('添加成功', Fun::url(['isuserinfo/index']), true);
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }


    public function actionUpdate()
    {
        $this->layout = false;
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('backend', '信息不存在'));
        }
        $this->layout = '@app/views/layouts/iframe.php';
        $model = IsuserInfo::findOne(\Yii::$app->request->get('id'));
        if ($model == null) {
            throw new BadRequestHttpException(\Yii::t('backend', '信息不存在'));
        }
        $password = $model->password;
        $model->password = '';
        $form = new BuildForm('@app/components/forms/isuserinfoForm.php');
        $form->loadModel(['isuserinfo' => $model]);
        $form->loadDefaultValues();
        $admin = \zhtfrontend\models\datas\Admin::find()->where(['role' => ['admin', 'administrator', 'business_director']])->all();
        $form->setItems('isuserinfo', 'response_man', ArrayHelper::map($admin, 'id', 'username'));
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            $model->user_id = \Yii::$app->user->id;
            $model->site_id = Fun::getSiteId();
            if ($model->password == null) {
                $model->password = $password;
            } else {
                $model->password = md5($model->password);
            }
            if ($model->save(false)) {
                return $this->redirectMessage(\Yii::t('backend', '修改成功'), Fun::url(['isuserinfo/index']), 'success');
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }

    public function actionDelete()
    {
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('backend', '信息不存在'));
        }
        $model = IsuserInfo::findOne(\Yii::$app->request->get('id'));
        if ($model == null) {
            throw new BadRequestHttpException(\Yii::t('backend', '信息不存在'));
        }
        if (!empty($model->customer_id)) {
            Customer::updateAll(['is_sender' => 0], ['id' => $model->customer_id]);
        }
        $model->status = 2;
        if ($model->save(false)) {
            return $this->redirectMessage(\Yii::t('backend', '删除成功'), Fun::url(['isuserinfo/index']), 'success');
        }
    }
}

?>